package tcln.gxtx.client.utils.date;

/**
 * Abstract class for an instant of date in GWT.
 * 
 * @author Truong Chau Lien Ngoc (lienngoc@gmail.com)
 */
public abstract class GwtDateInstant implements Comparable<GwtDateInstant> {

    // to hide
    protected GwtDateInstant() {
    }

    /**
     * Checks if this instant is after given instant according to chronological order.
     * 
     * @param another
     * @return <code>true</code> if this instant is after given instant.
     */
    protected boolean isAfter(GwtDateInstant another) {
        return this.compareTo(another) > 0;
    }

    /**
     * Checks if this instant is before given instant according to chronological order.
     * 
     * @param another
     * @return <code>true</code> if this instant is before given instant.
     */
    protected boolean isBefore(GwtDateInstant another) {
        return this.compareTo(another) < 0;
    }

    /**
     * Checks if this instant is equal to given instant according to chronological order.
     * 
     * @param another
     * @return <code>true</code> if this instant = given instant.
     */
    protected boolean isEqualTo(GwtDateInstant another) {
        return this.compareTo(another) == 0;
    }

    /**
     * Checks if this instant is in rage [<i>lower</i>, <i>upper</i>] according to chronological order.
     * 
     * @param lower
     * @param upper
     * @return <code>true</code> if this instant in [<i>lower</i>, <i>upper</i>].
     */
    protected boolean isBetween(GwtDateInstant lower, GwtDateInstant upper) {
        return this.compareTo(lower) >= 0 && this.compareTo(upper) <= 0;
    }

    /**
     * Checks if this instant is in rage (<i>lower</i>, <i>upper</i>) according to chronological order. This means, if
     * this instant = <i>lower</i> or <i>upper</i>, this method should return <code>false</code>.
     * 
     * @param lower
     * @param upper
     * @return <code>true</code> if this instant in [<i>lower</i>, <i>upper</i>].
     */
    protected boolean isBetweenExclusive(GwtDateInstant lower, GwtDateInstant upper) {
        return this.compareTo(lower) > 0 && this.compareTo(upper) < 0;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        } else if (obj instanceof GwtDateInstant) {
            return isEqualTo((GwtDateInstant) obj);
        } else {
            return super.equals(obj);
        }
    }
}
